package lab03.Test09;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/12/13.
 */
public class Client {
    public static void main(String[] args) {
        //txt文件内容->List
        //单步调试
        //断点：程序执行到这一点就不执行了
        //F8执行下一步，debug，+号查看对应值
        //选择可能出错的位置，小病毒点击
        /*
        * 错误：
        * 1.编译错误：idea会给非常明显的红色错误，亦即
        * 2.运行时错误：idea没有红标记，但是运行的时候就出现了错误。
        * */
        List<Book> books = new ArrayList<Book>();

        String path = "D:\\book.txt";
        String path1 = "D:\\book1.txt";
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(path));
            writer = new BufferedWriter(new FileWriter(path1));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] tokens = line.split("\t");
                Book book = new Book();
                book.bookNo = Integer.parseInt(tokens[0]);//int
                book.bookName = tokens[1];//string
                book.version = tokens[2];//string
                book.price = Double.parseDouble(tokens[3]);//double
                book.sales = Double.parseDouble(tokens[4]);//double
                books.add(book);
            }//添加book类
            int i = 1;
            for (Book b : books) {

                if (i == 3) {
                    b.price = b.price + 20;
                }
                if (i == 4) {
                    b.price = b.price + 30;
                }
                i++;
            }


            for (Book b : books) {
                writer.write(b.bookNo + "  "
                        + b.bookName + "    "
                        + b.version + "   "
                        + b.price + " "
                        + b.sales);
                writer.newLine();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
